package admin

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"new_club_recruitment/global"
	"new_club_recruitment/model/admin"
	"new_club_recruitment/model/admin/request"
	common "new_club_recruitment/model/common/request"
	"new_club_recruitment/model/common/response"
	"new_club_recruitment/utils"
)

type AdminApi struct{}

//GetAdmins 获取管理员信息
func (a *AdminApi) GetAdmins(c *gin.Context) {
	//1.解析参数
	var pageInfo request.AdminSearch
	if err := c.ShouldBindQuery(&pageInfo); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMessage("获取失败", c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if list, total, err := adminSerice.GetAdminInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		//3.返回响应
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.PageNum,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// DelAdmins 删除管理员信息
func (a *AdminApi) DelAdmins(c *gin.Context) {
	//1.解析参数
	id := c.Param("id")
	//2.处理业务
	if err := adminSerice.DelAdminInfo(id); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.Ok(c)
	}
}

// AddAdmins 添加管理员信息
func (a *AdminApi) AddAdmins(c *gin.Context) {
	//1.解析参数
	var info admin.SysAdmin
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	info.Password = utils.BcryptHash("123456")
	//2.处理业务
	if err := adminSerice.AddAdminInfo(info); err != nil {
		global.GVA_LOG.Error("添加失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.Ok(c)
	}
}

// UpdateAdmins 修改管理员信息
func (a *AdminApi) UpdateAdmins(c *gin.Context) {
	//1.解析参数
	var info admin.SysAdmin
	if err := c.ShouldBind(&info); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if err := adminSerice.UpdateAdminInfo(info); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.Ok(c)
	}
}

//GetExaAdmin 获取单一管理员信息
func (a *AdminApi) GetExaAdmin(c *gin.Context) {
	id := c.Param("id")
	//业务处理
	data, err := adminSerice.GetAdminInfo(id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.Fail(c)
	} else {
		response.OkWithData(data, c)
	}
}

// Login 登陆
func (a *AdminApi) Login(c *gin.Context) {
	var info common.Info
	if err := c.ShouldBind(&info); err != nil {
		fmt.Println(info)
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.Fail(c)
			return
		}
		response.FailWithMessage(errs.Translate(global.GAV_TRANS), c)
		return
	}
	//2.处理业务
	if data, url, err := adminSerice.Login(info); err != nil {
		global.GVA_LOG.Error("登陆失败!", zap.Error(err))
		response.Fail(c)
	} else {
		//3.返回响应
		response.OkWithDetailed(gin.H{"admin": data, "url": url}, "登陆成功", c)
	}
}
